package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.math.MathUtils;
import com.ruoyi.system.constant.ProjectStatus;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.MstnSchemeComputeMapper;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.mapper.MstnCatalystsMapper;

/**
 * 催化剂Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-10
 */
@Service
public class MstnCatalystsServiceImpl implements IMstnCatalystsService 
{
    @Autowired
    private MstnCatalystsMapper mstnCatalystsMapper;

    @Autowired
    private IMstnCatalystsService mstnCatalystsService;

    @Autowired
    private IMstnSchemeComputeResultService mstnSchemeComputeResultService;

    @Autowired
    private IMstnSchemeBatchService mstnSchemeBatchService;
    @Autowired
    private IMstnSchemeComputeService mstnSchemeComputeService;

    /**
     * 查询催化剂
     * 
     * @param catalystsId 催化剂主键
     * @return 催化剂
     */
    @Override
    public MstnCatalysts selectMstnCatalystsByCatalystsId(Long catalystsId)
    {
        return mstnCatalystsMapper.selectMstnCatalystsByCatalystsId(catalystsId);
    }

    /**
     * 选择催化剂[通过高度]
     * @return
     */
    @Override
    public MstnCatalysts selectCatalystsByNextHeight(String categoryName, String catalystsName, BigDecimal h){
        return mstnCatalystsMapper.selectCatalystsByNextHeight(categoryName, catalystsName, h);
    }
    /**
     * 选择催化剂
     * @return
     */
    @Override
    public void selectMstnCatalystsByCompute(MstnSchemeBatch mstnSchemeBatch){

        MstnSchemeBatch dbSchemeBatch = mstnSchemeBatchService.selectMstnSchemeBatchByBatchId(mstnSchemeBatch.getBatchId());
        //选择对应型号的所有催化剂
        List<CatalystsMiddleResult> catalystsList = mstnCatalystsService.selectMstnCatalystsWithChartsList( new CatalystsMiddleResult(dbSchemeBatch.getCatalystsName(),dbSchemeBatch.getCategoryName(), dbSchemeBatch.getLifetime()));
        MstnSchemeCompute searchSchemeCompute = new MstnSchemeCompute();
        searchSchemeCompute.setBatchId(mstnSchemeBatch.getBatchId());
        List<MstnSchemeCompute> list = mstnSchemeComputeService.selectMstnSchemeComputeList(searchSchemeCompute);

        for (MstnSchemeCompute mstnSchemeCompute : list){
            for (CatalystsMiddleResult catalysts : catalystsList){
                //寿命到期时的脱销效率
                BigDecimal efficiency = MathUtils.efficiency(mstnSchemeCompute.getNox(), dbSchemeBatch.getOutNox());
                BigDecimal zv = MathUtils.zv(mstnSchemeCompute.getQuantityFlow(),catalysts.getK(),catalysts.getGeometryAreaRate(),efficiency);
                if (zv.compareTo(new BigDecimal(0)) <= 0){
                    continue;
                }
                BigDecimal s = MathUtils.s(mstnSchemeCompute.getQuantityFlow());
                if (s.compareTo(new BigDecimal("0"))==0){
                    continue;
                }
                BigDecimal h = MathUtils.totalH(zv,s);
                if (!MathUtils.check(mstnSchemeBatch.getMatrixX(),mstnSchemeBatch.getMatrixY(),s)){
                    continue;
                }
                BigDecimal cs = new BigDecimal(mstnSchemeBatch.getFloors());
                BigDecimal singleH = MathUtils.singleH(h,cs);
                //型号匹配
                if (MathUtils.switchCatalystsType(singleH).equals(catalysts.getModuleName())){
                    MstnSchemeComputeResult mstnSchemeComputeResult = new MstnSchemeComputeResult();
                    mstnSchemeComputeResult.setProjectId(dbSchemeBatch.getProjectId());
                    //工况
                    mstnSchemeComputeResult.setConditionType(mstnSchemeCompute.getConditionType());
                    //催化剂id
                    mstnSchemeComputeResult.setCatalystsId(catalysts.getCatalystsId());
                    //型号
                    mstnSchemeComputeResult.setCategoryName(catalysts.getCategoryName());
                    //催化剂名称
                    mstnSchemeComputeResult.setCatalystsName(catalysts.getCatalystsName());
                    //每台催化剂体积  V=ZV/JZ
                    mstnSchemeComputeResult.setCatalystsVolume(zv.divide(new BigDecimal(dbSchemeBatch.getGroupCount()),2 , RoundingMode.HALF_UP));
                    //机组数量 JZ
                    mstnSchemeComputeResult.setGroupCount(dbSchemeBatch.getGroupCount());
                    //催化剂总体积
                    mstnSchemeComputeResult.setCatalystTotalVolume(zv);
                    //每台机组反应器数
                    mstnSchemeComputeResult.setReactorCount(dbSchemeBatch.getReactorCount());
                    //每台反应器催化剂体积 DV = V /FYQ
                    mstnSchemeComputeResult.setSingleReactorCatalystsVolume(mstnSchemeComputeResult.getCatalystsVolume().divide(new BigDecimal(dbSchemeBatch.getReactorCount()), 2 , RoundingMode.HALF_UP));
                    //催化剂层数
                    mstnSchemeComputeResult.setReactorFloor(mstnSchemeBatch.getFloors());
                    //催化剂单元阵列
                    mstnSchemeComputeResult.setUnitMatrixCount(mstnSchemeBatch.getMatrixX()+"*"+mstnSchemeBatch.getMatrixY());
                    //每台反应器每层催化剂单元数量
                    mstnSchemeComputeResult.setMinReactorUnitVolume(mstnSchemeBatch.getMatrixX() * mstnSchemeBatch.getMatrixY());
                    //模块型号
                    mstnSchemeComputeResult.setModuleName(catalysts.getModuleName());
                    //模块尺寸
                    mstnSchemeComputeResult.setModuleSize(catalysts.getModuleL().toString() + "*" + catalysts.getModuleW().toString() + "*" + catalysts.getModuleH().toString());
                    //每层净催化剂高度
                    mstnSchemeComputeResult.setCatalystsHeight(catalysts.getCatalystsH());
                    //反应器尺寸
                    mstnSchemeComputeResult.setReactorSize(mstnSchemeBatch.getReactorL() + "*" + mstnSchemeBatch.getReactorW());
                    //每个模块的毛重
                    mstnSchemeComputeResult.setWeight(catalysts.getModuleKg());
                    mstnSchemeComputeResult.setErrorRate(catalysts.getErrorRate());
                    //每台反应器内催化剂模块总数
                    mstnSchemeComputeResult.setSingleReactorUnitCount(mstnSchemeBatch.getMatrixX() * mstnSchemeBatch.getMatrixY() * mstnSchemeBatch.getFloors());
                    //每台反应器内模块的总毛总
                    mstnSchemeComputeResult.setTotalWeight(catalysts.getModuleKg().multiply(new BigDecimal(mstnSchemeComputeResult.getSingleReactorUnitCount())));
                    mstnSchemeComputeResult.setRemark(mstnSchemeCompute.getRemark());
                    mstnSchemeComputeResult.setPrecondition(mstnSchemeCompute.getPrecondition());
                    mstnSchemeComputeResult.setBatchId(mstnSchemeBatch.getBatchId());
                    mstnSchemeComputeResultService.insertMstnSchemeComputeResult(mstnSchemeComputeResult);
                }
            }

        }

        //更新项目状态
        mstnSchemeBatch.setProjectStatus(ProjectStatus.PROJECT_STATUS_TECHNICAL);
        mstnSchemeBatchService.updateMstnSchemeBatch(mstnSchemeBatch);
    }
    /**
     * 查询催化剂列表
     * 
     * @param mstnCatalysts 催化剂
     * @return 催化剂
     */
    @Override
    public List<MstnCatalysts> selectMstnCatalystsList(MstnCatalysts mstnCatalysts)
    {
        return mstnCatalystsMapper.selectMstnCatalystsList(mstnCatalysts);
    }

    /**
     * 查询催化剂列表[带子表数据]
     *
     * @param mstnCatalysts 催化剂
     * @return 催化剂集合
     */
    @Override
    public List<CatalystsMiddleResult> selectMstnCatalystsWithChartsList(CatalystsMiddleResult mstnCatalysts){
        return mstnCatalystsMapper.selectMstnCatalystsWithChartsList(mstnCatalysts);
    }

    @Override
    public CatalystsMiddleResult selectMstnCatalystsArea(CatalystsMiddleResult mstnCatalysts) {
        return mstnCatalystsMapper.selectMstnCatalystsArea(mstnCatalysts);
    }

    /**
     * 新增催化剂
     * 
     * @param mstnCatalysts 催化剂
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMstnCatalysts(MstnCatalysts mstnCatalysts)
    {
        mstnCatalysts.setCreateTime(DateUtils.getNowDate());
        int rows = mstnCatalystsMapper.insertMstnCatalysts(mstnCatalysts);
        insertMstnCatalystsCharts(mstnCatalysts);
        return rows;
    }

    /**
     * 修改催化剂
     * 
     * @param mstnCatalysts 催化剂
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMstnCatalysts(MstnCatalysts mstnCatalysts)
    {
        mstnCatalysts.setUpdateTime(DateUtils.getNowDate());
        mstnCatalystsMapper.deleteMstnCatalystsChartsByCatalystsId(mstnCatalysts.getCatalystsId());
        insertMstnCatalystsCharts(mstnCatalysts);
        return mstnCatalystsMapper.updateMstnCatalysts(mstnCatalysts);
    }

    /**
     * 批量删除催化剂
     * 
     * @param catalystsIds 需要删除的催化剂主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMstnCatalystsByCatalystsIds(Long[] catalystsIds)
    {
        mstnCatalystsMapper.deleteMstnCatalystsChartsByCatalystsIds(catalystsIds);
        return mstnCatalystsMapper.deleteMstnCatalystsByCatalystsIds(catalystsIds);
    }

    /**
     * 删除催化剂信息
     * 
     * @param catalystsId 催化剂主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMstnCatalystsByCatalystsId(Long catalystsId)
    {
        mstnCatalystsMapper.deleteMstnCatalystsChartsByCatalystsId(catalystsId);
        return mstnCatalystsMapper.deleteMstnCatalystsByCatalystsId(catalystsId);
    }

    /**
     * 新增催化剂图信息
     * 
     * @param mstnCatalysts 催化剂对象
     */
    public void insertMstnCatalystsCharts(MstnCatalysts mstnCatalysts)
    {
        List<MstnCatalystsCharts> mstnCatalystsChartsList = mstnCatalysts.getMstnCatalystsChartsList();
        Long catalystsId = mstnCatalysts.getCatalystsId();
        if (StringUtils.isNotNull(mstnCatalystsChartsList))
        {
            List<MstnCatalystsCharts> list = new ArrayList<MstnCatalystsCharts>();
            for (MstnCatalystsCharts mstnCatalystsCharts : mstnCatalystsChartsList)
            {
                mstnCatalystsCharts.setCatalystsId(catalystsId);
                list.add(mstnCatalystsCharts);
            }
            if (list.size() > 0)
            {
                mstnCatalystsMapper.batchMstnCatalystsCharts(list);
            }
        }
    }


}
